package com.newbee.activity.tab;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hhu.newbee.R;
import com.newbee.activity.LoginActivity;
import com.newbee.activity.MultChatChooseFriend;
import com.newbee.adapter.QunZuAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.app.SpeechApp;
import com.newbee.fragement.multchat.MultChatFragement;
import com.newbee.listbean.QunZuInfoBean;
import com.newbee.multchat.MultChatActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.util.MD5Util;

public class NewQunZu extends Activity implements OnRefrushActivity,
		OnItemClickListener, OnClickListener
{
	private ListView mListView;

	private List<QunZuInfoBean> mlist;

	private QunZuAdapter madapter;

	private TextView emptyTextView;

	private long mExitTime;

	private Builder builder;

	private ImageButton addQunzu;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acitivity_newqunzu);
		emptyTextView = (TextView) this.findViewById(R.id.qunzulist_empty);
		emptyTextView.setOnClickListener(this);
		mListView = (ListView) this.findViewById(R.id.qunzulist);
		mListView.setOnItemClickListener(this);
		SpeechApp.add(this);
		addQunzu = (ImageButton) this.findViewById(R.id.btn_addqunzu);
		addQunzu.setOnClickListener(this);
		builder = new AlertDialog.Builder(this);
		builder.setMessage("亲，请先登录或注册！");
		builder.setTitle("提醒");
		builder.setPositiveButton("登录", new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick(DialogInterface arg0, int arg1)
			{

			}
		});

	}

	@Override
	protected void onResume()
	{
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences sp = this.getSharedPreferences(
				MainActivity.SP_USR_LOGIN_STATUS, Activity.MODE_MULTI_PROCESS);

		boolean isUserLog = sp.getBoolean(MainActivity.LOGIN_STATUE, false);

		if (isUserLog)
		{
			addQunzu.setVisibility(View.VISIBLE);
			MainServiceTask mst = new MainServiceTask(TaskType.GET_QUNZU, null,
					this.getClass().getName());
			MainService.addActivityMap(this.getClass().getName(), this);
			MainService.addTaskQueue(mst);
			MainService.addSpecialActivity(this.getClass().getName(), this);
		}
		else
		{

			// this.builder.create().show();

			userNoLogSet();
		}

	}

	private void userNoLogSet()
	{
		emptyTextView.setText("您未登录，请点击登录！");
		mListView.setEmptyView(emptyTextView);
		addQunzu.setVisibility(View.GONE);
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.GET_QUNZU:
		{

			this.mlist = (List<QunZuInfoBean>) paras[0];
			if (mlist.size() == 0)
			{
				Log.d("wch", "-------------------->");
				emptyTextView.setText("您的房间列表为空");
				mListView.setEmptyView(emptyTextView);

			}
			else if (madapter == null)
			{
				this.madapter = new QunZuAdapter(this, mlist);
				this.mListView.setAdapter(this.madapter);
			}
			else
			{
				this.madapter.setList(mlist);
				this.madapter.notifyDataSetChanged();
			}
			break;
		}
		case TaskType.MULT_CHAT_MSG:
		{

			// String roomName = (String) paras[0];
			//
			// updateListview(roomName);
			break;
		}

		case TaskType.CREATE_MULT_CHAT:
		{

			String qunzuName = (String) paras[0];

			List<String> userlist = (List<String>) paras[1];

			List<String> rl = new ArrayList<String>();
			for (String s : userlist)
			{
				rl.add(s.split("@")[0]);

			}
			mlist.add(new QunZuInfoBean(qunzuName, rl));
			if (this.madapter == null)
			{

				this.madapter = new QunZuAdapter(this, mlist);
				this.mListView.setAdapter(madapter);
			}
			else
			{
				this.madapter.notifyDataSetChanged();
			}

			break;
		}

		case TaskType.ACCEPT_MULT:
		{

		}
		default:
			break;
		}
		return null;

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3)
	{

		Log.d("WCH", "群组itme click");
		QunZuInfoBean bean = this.mlist.get(p);
		String qunzuName = bean.getQunzuName();
		// 要注意当前类为fragment而不是activity；
		Intent intent = new Intent(this, MultChatActivity.class);
		Bundle bundle = new Bundle();
		bundle.putCharSequence(MultChatFragement.CHAT_ROOM_MAME,
				MD5Util.multChatRoom(qunzuName));
		bundle.putStringArrayList(MultChatFragement.USERS_LIST,
				usersPauser(bean.getUserList()));

		bundle.putString(MultChatChooseFriend.QZ_NAME, qunzuName);

		intent.putExtras(bundle);
		this.startActivity(intent);

	}

	private ArrayList<String> usersPauser(List<String> users)
	{

		ArrayList<String> list = new ArrayList<String>();
		for (String s : users)
		{

			list.add(s + "@" + SmackClient.SERVER_NAME);
		}

		return list;
	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(this.getClass().getName());
		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{

		switch (v.getId())
		{
		case R.id.btn_addqunzu:
		{
			this.startActivity(new Intent(this, MultChatChooseFriend.class));
			break;
		}
		case R.id.qunzulist_empty:
		{
			SharedPreferences sp = this.getSharedPreferences(
					MainActivity.SP_USR_LOGIN_STATUS,
					Activity.MODE_MULTI_PROCESS);

			boolean isUserLog = sp.getBoolean(MainActivity.LOGIN_STATUE, false);
			if (isUserLog)
			{
				// do nothing
			}
			else
			{
				Intent intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
			}
		}
		default:
			break;
		}

	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if ((System.currentTimeMillis() - mExitTime) > 2000)
			{
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();

				return true;
			}
			else
			{
				SpeechApp.exit();
				return true;
			}

		}
		return super.onKeyDown(keyCode, event);
	}
}
